//
///////

#include "../Header/WebEffectServer.h"

using namespace std;

WebEffectServer::WebEffectServer()
{
	record_flag = false;
	//direct_eff_vector = vector;
	//proxy_eff_vector = vector();
	//no_eff_vector = vector();
	str_store_file_name = "WebServerEffer";
	InitServer();
}

WebEffectServer::~WebEffectServer()
{
	WriteToFile();
}

bool WebEffectServer::InitServer()
{
	ifstream in_file(str_store_file_name.c_str());
	if (!in_file) {
		ofstream out_file(str_store_file_name.c_str());
		out_file.close();
		in_file.close();
		cout << "create a server store file: " << str_store_file_name << endl
			 << "Now the server has no record!" << endl;
	}
	else {
		cout << "read the record from the file" << str_store_file_name << endl;
		ReadFileRecord();
	}
	return true;
}

void WebEffectServer::InsertDirectEffectWebAddr(const string & str_web_addr)
{
	if (str_web_addr.empty())
		return;
	EraseRecord(str_web_addr);
	for(vector<string>::iterator iter = direct_eff_vector.begin(); 
		iter != direct_eff_vector.end(); iter++) {
			if (*iter == str_web_addr)
				return;
	}
	direct_eff_vector.push_back(str_web_addr);
	record_flag = true;
}

void WebEffectServer::InsertProxyEffectWebAddr(const string & str_web_addr)
{
	if (str_web_addr.empty())
		return;
	EraseRecord(str_web_addr);
	for(vector<string>::iterator iter = proxy_eff_vector.begin(); 
		iter != proxy_eff_vector.end(); iter++) {
			if (*iter == str_web_addr)
				return;
	}
	proxy_eff_vector.push_back(str_web_addr);
	record_flag = true;
}

void WebEffectServer::InsertNoEffectWebAddr(const string & str_web_addr)
{
	if (str_web_addr.empty())
		return;
	EraseRecord(str_web_addr);
	for(vector<string>::iterator iter = no_eff_vector.begin(); 
		iter != no_eff_vector.end(); iter++) {
			if (*iter == str_web_addr)
				return;
	}
	no_eff_vector.push_back(str_web_addr);
	record_flag = true;
}

void WebEffectServer::InsertNoDnsEffWebAddr(const string & str_web_addr)
{
	if (str_web_addr.empty())
		return;
	EraseRecord(str_web_addr);
	for(vector<string>::iterator iter = no_dns_eff_vector.begin(); 
		iter != no_dns_eff_vector.end(); iter++) {
			if (*iter == str_web_addr)
				return;
	}
	no_dns_eff_vector.push_back(str_web_addr);
	record_flag = true;
}

void WebEffectServer::WriteToFile()
{
	if (!record_flag)
		return;
	EmptyFile();
	WriteNoDnsEffVectorToFile();
	WriteDirectVectorToFile();
	WriteProxyVectorToFile();
	WriteNoEffVectorToFile();
}

void WebEffectServer::WriteDirectVectorToFile()
{
	ofstream out_file(str_store_file_name.c_str(), ofstream::app);
	out_file << "direct to get photo through education web: "<< direct_eff_vector.size() << endl;
	vector<string>::iterator it = direct_eff_vector.begin();
	for (; it != direct_eff_vector.end(); it ++) {
		out_file << (*it) << endl;
	}
	out_file << endl;
	out_file.close();
}

void WebEffectServer::WriteProxyVectorToFile()
{
	ofstream out_file(str_store_file_name.c_str(), ofstream::app);
	out_file << "get photo through proxy server: " << proxy_eff_vector.size() <<endl;
	for (vector<string>::iterator it = proxy_eff_vector.begin(); it != proxy_eff_vector.end(); it++) {
		out_file << (*it) << endl;
	}
	out_file << endl;
	out_file.close();
}

void WebEffectServer::WriteNoEffVectorToFile()
{
	ofstream out_file(str_store_file_name.c_str(), ofstream::app);
	out_file << "the web address is no effiect: " << no_eff_vector.size() << endl;
	for (vector<string>::iterator it = no_eff_vector.begin(); it != no_eff_vector.end(); it++) {
		out_file << (*it) << endl;
	}
	out_file << endl;
	out_file.close();
}

void WebEffectServer::WriteNoDnsEffVectorToFile()
{
	ofstream out_file(str_store_file_name.c_str(), ofstream::app);
	out_file << "the web address can`t find IP address: " << no_dns_eff_vector.size() << endl;
	for (vector<string>::iterator it = no_dns_eff_vector.begin(); it != no_dns_eff_vector.end(); it++) {
		out_file << (*it) << endl;
	}
	out_file << endl;
	out_file.close();
}

void WebEffectServer::EmptyFile()
{
	ofstream out_file(str_store_file_name.c_str());
	out_file.close();
}

void WebEffectServer::ReadFileRecord()
{
	ifstream in_file(str_store_file_name.c_str());
	string tmp;
	while(getline(in_file, tmp)) {
		if (tmp.find("direct") != string::npos) {
			string::size_type pos = tmp.find(":");
			string str_num = tmp.substr(pos + 2, tmp.size() - pos - 2);
			int i = atoi(str_num.c_str());
			for (int j = 0; j < i; ++j) {
				getline(in_file, tmp);
				InsertDirectEffectWebAddr(tmp);
			}
			cout << "read direct web addr: " << i << endl;
		}
		else if (tmp.find("no effiect") != string::npos) {
			string::size_type pos = tmp.find(":");
			string str_num = tmp.substr(pos + 2, tmp.size() - pos - 2);
			int i = atoi(str_num.c_str());
			for (int j = 0; j < i; ++j) {
				getline(in_file, tmp);
				InsertNoEffectWebAddr(tmp);
			}
			cout << "read no eff web addr: " << i << endl;
		}
		else if (tmp.find("proxy server") != string::npos) {
			string::size_type pos = tmp.find(":");
			string str_num = tmp.substr(pos + 2, tmp.size() - pos - 2);
			int i = atoi(str_num.c_str());
			for (int j = 0; j < i; ++j) {
				getline(in_file, tmp);
				InsertProxyEffectWebAddr(tmp);
			}
			cout << "read proxy web addr: " << i << endl;
		}
		else if (tmp.find("IP address") != string::npos) {
			string::size_type pos = tmp.find(":");
			string str_num = tmp.substr(pos + 2, tmp.size() - pos - 2);
			int i = atoi(str_num.c_str());
			for (int j = 0; j < i; ++j) {
				getline(in_file, tmp);
				InsertNoDnsEffWebAddr(tmp);
			}
			cout << "read no dns ip web addr: " << i << endl;
		}
		getline(in_file, tmp);
	}
	in_file.close();
	record_flag = true;
}

int  WebEffectServer::FindRecordExist(string & web_addr)
{
	if (record_flag == false) 
		return NO_FIND;

	vector<string>::iterator iter = proxy_eff_vector.begin();
	for(; iter != proxy_eff_vector.end(); ++iter) {
		if (*iter == web_addr)
			return PROXY_ADDR;
	}

	iter = no_dns_eff_vector.begin();
	for(; iter != no_dns_eff_vector.end(); ++iter) {
		if (*iter == web_addr)
			return NO_IP;
	}

	iter = direct_eff_vector.begin();
	for(; iter != direct_eff_vector.end(); ++iter) {
		if (*iter == web_addr)
			return DIRECT_ADDR;
	}

	iter = no_eff_vector.begin();
	for(; iter != no_eff_vector.end(); ++iter) {
		if (*iter == web_addr)
			return NOEFF_ADDR;
	}
	return NO_FIND;
}

void WebEffectServer::EraseRecord(const string & web_addr) 
{
	vector<string>::iterator iter = proxy_eff_vector.begin();
	for(; iter != proxy_eff_vector.end(); ) {
		if (*iter == web_addr) {
			proxy_eff_vector.erase(iter);
			break;
		}
		++iter;
	}

	iter = no_dns_eff_vector.begin();
	for(; iter != no_dns_eff_vector.end(); ) {
		if (*iter == web_addr) {
			no_dns_eff_vector.erase(iter);
			break;
		}
		++iter;
	}

	iter = direct_eff_vector.begin();
	for(; iter != direct_eff_vector.end(); ) {
		if (*iter == web_addr) {
			direct_eff_vector.erase(iter);
			break;
		}
		++iter;
	}

	iter = no_eff_vector.begin();
	for(; iter != no_eff_vector.end(); ) {
		if (*iter == web_addr) {
			no_eff_vector.erase(iter);
			break;
		}
		++iter;
	}
}